/*
  Creator: Kisamin Resident and copyright by her.  All rights reserved except those specifically set forth here:
  You may:
  	Use these scripts in any project
	Modify these scripts in any way you wish subject to:
		1. Retaining this set of comments
		2. Explicity commenting and showing any changes you might make
	You may NOT:
	License these scripts to someone else with restrictions differing from the above.
	Charge a fee for the scripts themselves (although you may charge for an object that
		utilizes these scripts to add functionality)
	Charge a fee for any "library" or "collection" of scripts that includes this script.

If you have questions about your rights under this coment and/or the Gnu CopyLeft, please request further information
and/or explicit permission from Kisamin Resident

This script is not intended to be efficient or low lag.  It is merely a tst harness for unit testing of components of the missile
system.
*/

key g_kSpecifiedTarget;

vector k_vRezOffset = <3., 0., 0.>;
vector k_vRezSpeed = <1., 0., 0.>;

integer g_iMenuChannel = 0;
integer g_iMenuListenerHandle = 0;

integer g_iSpeedChannel;
integer g_iSpeedListenerHandle;

integer g_iTargetChannel;
integer g_iTargetListenerHandle;

integer myChannel () {
  return (integer)("0x" + llGetSubString ((string)llGetOwner (), 0, 7));
}




doShoot () {
  llRezObject (g_sMunitionName, 
			   llGetRootPosition () + (k_vRezOffset * llGetRootRotation ()),
			   k_vRezSpeed * llGetRootRotation (),
			   llGetRootRotation (),
			   100);
  llOwnerSay ("Launched " + g_sMunitionName);
}




doMenu (string in_sSpeaker, key in_kSpeaker, string in_sMessage) {
  if (g_iMenuListenerHandle)
	llListenRemove (g_iMenuListenerHandle);
  g_iMenuListenerHandle = 0;

  if (in_sMessage == "Shoot") {
	doShoot ();
  } else if (in_sMessage == "Speed") {
  } else if (in_sMessage == "Offset") {
  } else if (in_sMessage == "Target") {
	list l_lAgents = llGetAgentList (AGENT_LIST_PARCEL, []);
	if (llGetListEntryType (l_lAgents, 0) == TYPE_STRING) {
	  llOwnerSay (llList2String (l_lAgents, 0));
	  return;
	} else {
	  integer i;
	  list l_lButtons;
	  integer l_iNumAgents = llGetListLength (l_lAgents);
	  if (l_iNumAgents > 12)
		l_iNumAgents = 12;

	  for (i=0; i<llGetListLength (l_lAgents) i++) {
		key l_kPossibleTarget = llList2Key (l_lAgents, i);
		string l_sName = llKey2Name (l_kPossibleTarget);
		l_lButtons += l_sName;
	  }

	  g_iMenuListenerHandle = llListen (g_iMenuChannel, "", llGetOwner (), "");
	  llDialog (llGetOwner (), "Select Target:", l_lButtons, g_iMenuChannel);
	  llSetTimerEvent (30.);
	}
  }
}


string g_sMunitionName;

 default {
   changed (integer in_iChange) {
	 if (in_iChange & (CHANGED_INVENTORY | CHANGED_OWNER))
	   llResetScript ();
   }

   state_entry () {
	 llListen (myChannel (), "", NULL_KEY, "");

	 // Determine the munitions name including the maximum revision number

	 string l_sLastNumber;
	 string l_sMaxNameNumber;
	 integer l_iMaxNameNumber = 0;

	 integer i;
	 for (i=0; i<llGetInventoryNumber (INVENTORY_OBJECT); i++) {
	   string l_sMunitionName = llGetInventoryName (INVENTORY_OBJECT, i);
	   string l_sNameNumber;
	   if ( g_sMunitionName == "" || llSubStringIndex (g_sMunitionName, l_sMunitionName) == 0) {
		 if (g_sMunitionName != "")
		   l_sNameNumber = llDeleteSubString (g_sMunitionName, 0, llStringLength (l_sMunitionName));
		 g_sMunitionName = l_sMunitionName;
	   } else if (llSubStringIndex (l_sMunitionName, g_sMunitionName) == 0)
		 l_sNameNumber = llDeleteSubString (l_sMunitionName, 0, llStringLength (g_sMunitionName));
	   
	   l_sNameNumber = llStringTrim (l_sNameNumber, STRING_TRIM);
	   integer l_iNameNumber = (integer)l_sNameNumber;
	   if (l_iNameNumber > l_iMaxNameNumber) {
		 l_iMaxNameNumber = l_iNameNumber;
		 l_sMaxNameNumber = l_sNameNumber;
	   }
	 }

	 if (l_iMaxNameNumber > 0)
	   g_sMunitionName = g_sMunitionName + " " + l_sMaxNameNumber;
	 llOwnerSay ("Using munition named " + g_sMunitionName);
   }

   listen (integer in_iChannel, string in_sSpeaker, key in_kSpeaker, string in_sMessage) {
	 if (in_iChannel == g_iMenuChannel) {
	   doMenu (in_iChannel, in_sSpeaker, in_kSpeaker, in_sMessage);
	 } else if (in_iChannel == g_iSpeedChannel) {
	 } else if (in_iChannel == g_iTargetChannel) {
	 } else if (in_iChannel == g_iRemoteChannel) {
	   if (g_kSpecifeidTarget == NULL_KEY) {
		 list l_lObjectDetails = llGetObjectDetails (in_kTarget, [OBJECT_OWNER]);
		 g_kSpecifiedTarget = llList2Key (l_lObjectDetails, 0);
	   }

	   doShoot ();
	 }
   }

   // In case the rezzed object needs targeting information
   object_rez (key in_kObjectKey) {
	 if (g_kSpecifiedTarget != NULL_KEY)
	   llRegionSay (myChannel (), g_kSpecifiedTarget);
	 else
	   llRegionSay (myChannel (), (string)llGetOwner ());
   }

   touch_start (integer in_iNumTouchers) {
	 g_iMyChannel = myChannel () + 1;
	 if (g_iMenuListenerHandle == 0)
	   g_iMenuListenerHandle = llListen (g_iMyChannel, "", llGetOwner (), "");

	 integer i;
	 for (i=0; i<in_iNumTouchers; i++)
	   if (llDetectedKey (i) == llGetOwner ()) {
		 llDialog (llGetOwner (), "Select Function:", [
													   "Shoot"
													   , "Speed"
													   , "Offset"
													   , "Target"
													   ], g_iMyChannel );
   }
 }
